#include "HostLink.h"

using namespace cm_nat;

class TestHostLink: public HostLinkListener
{
public:
    void onReceive(ByteBuffer* data) 
    {
        std::cout<< "HostLinkListener get a packet\n";

        SocketAddress* p_sa = (SocketAddress*)data->getData();
        std::cout<< p_sa->toString() <<"\n";
        std::cout<< data->Length();
        delete p_sa;
        data->decreaseRef();
    }

    void onCheckResult( HostLink* link, int result )
    {

    }
};

int main()
{
    SocketReactor reactor;
    reactor.setTimeout(Poco::Timespan(1,0,0,0,0));

    ByteBufferPool bpool;
    bpool.setup(10, 200);

    BaseTimer timer;
    timer.setup();

    Thread t;
    t.start(reactor);

    TestHostLink thl;

    HostLink hlink( &bpool, &reactor, &timer );
    hlink.registerListener( &thl );

    IPAddress ip("192.168.0.128");
    hlink.setup( ip );

    SocketAddress stunSa("stun.l.google.com","19302");

    hlink.checkConnection( stunSa );

    std::string a;
    ByteBuffer* p_data;

    while(1)
    {
        std::cin>>a;
        
        p_data = bpool.get(0);
        if( p_data ){
            p_data->increaseRef();
            p_data->setLength(20);
            hlink.send(p_data);
        }

    }
    return 0;
}